using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using IGlobalist.Cms.Common;
using IGlobalist.Cms.Bll;
using IGlobalist.Cms.Model;
using System.Collections.Generic;

namespace IGlobalist.Cms.Admin.Control
{
    public partial class FAQsList : AdminBaseUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                GridViewDataBind();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            IFAQ faq = new FAQ();

            IList<ICmsClass> cList = ClassController.GetClassListByClassTypeID((short)CmsObjectType.FAQ);
            ICmsClass clazz = new CmsClass();
            if (cList.Count > 0)
                clazz= cList[0];
            faq.Class = clazz;
            faq.ClassID = clazz.ID;    
            faq.Address = txtAddress.Text;
            faq.Answer = txtAnswer.Text;            
            faq.Author = 539;
            faq.CreatedDate = DateTime.Now;
            faq.Email = txtEmail.Text;
            faq.Question = txtQuestion.Text;
            FAQController.InsertFAQ(faq);
            GridViewDataBind();
        }

        protected void gvQuestion_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                long ID = Convert.ToInt64(e.CommandArgument);               
                if (e.CommandName == "answers")
                {
                    Answers(ID);
                }
                if (e.CommandName == "delanswers")
                {
                    FAQController.DeleteAd(ID);
                    GridViewDataBind();
                }
            }
            catch
            {
                
            }
        }

        private void Answers(long id)
        {
            layuot_question.Visible=false;
            layout_answers.Visible = true;
            IFAQ faq = FAQController.GetFAQ(id);
            ViewState["FAQ_Answers"] = faq;
            lblDisplayQuestion.Text= faq.Question;        
        }

        private void GridViewDataBind()
        {
            layuot_question.Visible = true;
            layout_answers.Visible = false;
            IList<IFAQ> answerlist = FAQController.GetFAQList();
            IList<IFAQ> list = new List<IFAQ>();
            foreach (IFAQ faq in answerlist)
            {
                if (string.IsNullOrEmpty(faq.Answer))
                    list.Add((IFAQ)faq);
            }
            
            gvQuestion.DataSource = list;
            gvQuestion.DataBind();                
        }

        public void AnswersFAQ(object sender, EventArgs e)
        {
            IFAQ faq = new FAQ();
            if (ViewState["FAQ_Answers"] != null)
            {
                faq = (IFAQ)ViewState["FAQ_Answers"];
                faq.Class = faq.Class;
                faq.ClassID = faq.ID;
                faq.EditedDate = DateTime.Now;
                faq.Answer = tinyMCEAnswers.Text;
                FAQController.UpdateFAQ(faq);
                GridViewDataBind();
            }
        }
    }
}